import sys
import cherrypy
import wsgiref.handlers
import simplejson as json
import os.path

from google.appengine.api import memcache

from app.handlers.role import RoleHandler
from app.handlers.user import UserHandler
from app.handlers.login import LoginHandler
from app.handlers.department import DepartmentHandler
from app.handlers.post import PostHandler
from app.handlers.employee import EmployeeHandler
from app.handlers.benefit import BenefitHandler
from app.handlers.payroll import PayrollHandler

from app.populate import Populate
#from cpcstuff import cpcallow

from tests.Test import TestHandler
from tests.TestPayroll import TestPayrollHandler

class Root(object):
	@cherrypy.expose
	def index(self):
		pass
		
	@cherrypy.expose
	def logout(self):
		cherrypy.session.delete()
		return json.dumps({'msg':'Success'})
		
if __name__ == '__main__':
	root = Root()	
	root.login = LoginHandler()
	root.role = RoleHandler()
	root.user = UserHandler()
	root.department = DepartmentHandler()
	root.post = PostHandler()
	root.employee = EmployeeHandler()
	root.benefit = BenefitHandler()
	root.payroll = PayrollHandler()
	
	root.tests = TestHandler()
	root.tests.payroll = TestPayrollHandler()
	
	current_dir = os.path.abspath(os.path.dirname(__file__))

	sys.modules['memcache'] = memcache
	 
	conf = {"/":{'tools.sessions.on':  True,
			'tools.sessions.storage_type': "memcached",
			'tools.sessions.servers': ['memcached://'],
			'tools.sessions.name': 'payroll_d_session_id',
			'tools.sessions.clean_thread': True,
			'tools.session.timeout': 10, # ten minute session timeout, not sure if this works
			'tools.etags.on': True,
            'tools.etags.autotags': True},
			'/static': {'tools.staticdir.on': True, 
						'tools.staticdir.dir': os.path.join(current_dir, 'static')}
			#'cpcallow.allow.on': True,
			}
	
	app = cherrypy.tree.mount(root,"/",conf)
	
	#if hasattr(app, 'toolboxes'):
		#app.toolboxes['cpcallow'] = cpcallow.cpcallowtools
		
	wsgiref.handlers.CGIHandler().run(app)